//
//  EMMineViewController.m
//  EasyMoney
//
//  Created by zh on 2019/12/14.
//  Copyright © 2019 zh. All rights reserved.
//

#import "EMMineViewController.h"
#import "EMNavBarView.h"
#import "EMMineCardView.h"
#import "EMMineRequestModel.h"
#import "EMMineResponseModel.h"
#import "EMMineItemView.h"
#import "EMMineLogoutView.h"
#import "EMLocationManager.h"

#define kMineRespData @"kMineRespData"

@interface EMMineViewController ()


@property (nonatomic, strong) WDPBaseScrollView *scrollView;
@property (nonatomic, strong) EMNavBarView *headerView;
@property (nonatomic, strong) EMMineCardView *cardView;
@property (nonatomic, strong) EMMineLogoutView *logoutView;

@property (nonatomic, strong) EMMineResponseModel *respModel;

@end

@implementation EMMineViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self request];
    [[EMLocationManager sharedManager] start];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    /*
     http://47.92.64.95/genloanh5/emoney/#/credit
     http://47.92.64.95/genloanh5/emoney/#/apply
     http://47.92.64.95/genloanh5/emoney/#/repay
     http://47.92.64.95/genloanh5/emoney/#/repay/funpay
     http://47.92.64.95/genloanh5/emoney/#/mine/record
     http://47.92.64.95/genloanh5/emoney/#/mine/qa
     */
    [self setupUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:NetClientRefreshTokenInvalid object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupUI
{
    _headerView = [[EMNavBarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, EMNavBarHeight)];
    _headerView.title = NSLocalizedString(@"我的", nil);
    [self.view addSubview:_headerView];
    _scrollView = [[WDPBaseScrollView alloc] initWithFrame:CGRectMake(0, EMNavBarHeight, SCREEN_WIDTH, (SCREEN_HEIGHT - EMTabBarHeight-EMTabBarHeight))];
    _scrollView.backgroundColor = ColorFromRGB(0xf8f8f8);
    [self.view addSubview:_scrollView];
    _cardView = [[EMMineCardView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 98)];
    [_cardView setCallback:^(NSInteger tag, UIView *view, id data) {
        ICEOpenUrl((NSString *)data)
    }];
    __weak typeof(self)weakSelf = self;
    _logoutView = [[EMMineLogoutView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
    [_logoutView setCallback:^(NSInteger tag, UIView *view, id data) {
        [weakSelf clickLogout];
    }];
    _logoutView.wdpMarginUp = 20;
    _logoutView.wdpMarginDown = 20;
    [_scrollView inputWithItemviews:@[_cardView,_logoutView]];
    
    EMMineResponseModel *resp = (EMMineResponseModel *)[[EMCacheMananger shared] getLocalData:kMineRespData];
    if (resp) {
        [self refreshContentWithModel:resp];
    }
}

- (void)request
{
    EMMineRequestModel *req = [[EMMineRequestModel alloc] init];
    [req requestSuccess:^(EMMineResponseModel *response, NSString * _Nonnull message) {
        if (self.respModel == nil || [self.respModel isEqual:response] == NO)
        {
            [self refreshContentWithModel:response];
            [[EMCacheMananger shared] updateLocalData:response andModelName:kMineRespData];
        }
    } failure:^(NSInteger errorCode, NSString * _Nonnull errorMessage, NSError * _Nonnull error) {
        
    }];
}

- (void)refreshContentWithModel:(EMMineResponseModel *)model
{
    self.respModel = model;
    [_scrollView removeAllItemsView];
    [_cardView updateWithDataModel:model callBack:nil];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:model.tab.count+1];
    [array addObject:_cardView];
    for (EMMineTabItemModel *itemModel in model.tab) {
        EMMineItemView *item = [[EMMineItemView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
        [item updateWithDataModel:itemModel callBack:^(NSInteger tag, UIView *view, id data) {
            ICEOpenUrl((NSString *)data)
        }];
        [array addObject:item];
    }
    [array addObject:_logoutView];
    [_scrollView inputWithItemviews:array];
}

- (void)clickLogout
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"是否退出?", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"确定",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self logout];
    }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消",nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        return;
    }];
    [alertC addAction:okAction];
    [alertC addAction:cancelAction];
    [self presentViewController:alertC animated:YES completion:nil];
}

- (void)logout
{
    [EMUserInfoData clean];
    [[EMNetConfig sharedConfig] clearToken];
    EMPostNotification(EMLogoutNofitication);
    ICEOpenUrl(@"emoney://login?token=0");
}

@end
